/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.NotasAlunoDTO;
import app.entities.model.DTO.TurmaDTO;
import app.view.classes.ViewTurma;
import app.view.gui.listar.ListaTurma;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class LancarNotas extends javax.swing.JFrame {

    private String CodigoMatricula;
    private TurmaDTO turma;
    private ArrayList<NotasAlunoDTO> listaNotasAluno;
    private ViewTurma viewTurma;

    public LancarNotas(String CodigoMatricula) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.turma = new TurmaDTO();
        this.CodigoMatricula = CodigoMatricula;
        this.listaNotasAluno = new ArrayList<>();
        this.viewTurma = new ViewTurma();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        edtNomeMateria = new javax.swing.JTextField();
        btnSelecionarTurma = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        cbNumeroBimestre = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNotasAlunos = new javax.swing.JTable();
        btnLancarNotas = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Professor - Lançar Notas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Turma"));

        jLabel1.setText("Matéria");

        edtNomeMateria.setEditable(false);
        edtNomeMateria.setEnabled(false);

        btnSelecionarTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/conultar32x32.png"))); // NOI18N
        btnSelecionarTurma.setText("Selecionar Turma");
        btnSelecionarTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionarTurmaActionPerformed(evt);
            }
        });

        jLabel2.setText("Bimestre");

        cbNumeroBimestre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1º Bimestre", "2º Bimestre" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(edtNomeMateria, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbNumeroBimestre, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSelecionarTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(edtNomeMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cbNumeroBimestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnSelecionarTurma))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Notas Alunos"));

        tblNotasAlunos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Matricula", "Nome", "Sobrenome", "Nota"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblNotasAlunos);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 463, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnLancarNotas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/download.jpg"))); // NOI18N
        btnLancarNotas.setText("Lançar Notas");
        btnLancarNotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLancarNotasActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/images (2).jpg"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addComponent(btnLancarNotas)
                .addGap(53, 53, 53)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnLancarNotas))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSelecionarTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionarTurmaActionPerformed
        ListaTurma LT = new ListaTurma(this.tblNotasAlunos, this.edtNomeMateria, turma, CodigoMatricula);
        LT.setVisible(true);
    }//GEN-LAST:event_btnSelecionarTurmaActionPerformed

    private void btnLancarNotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLancarNotasActionPerformed
        if (this.tblNotasAlunos.getRowCount() <= 0) {
            JOptionPane.showMessageDialog(this, "Atenção! Essa turma não tem nenhum aluno matriculado",
                    "Lançar Notas", JOptionPane.INFORMATION_MESSAGE);
        } else {
            int numBimestre;
            int notasEmBranco = 0;
            DefaultTableModel model = (DefaultTableModel) this.tblNotasAlunos.getModel();
            for (int i = 0; i < model.getRowCount(); i++) {
                String notaAluno = String.valueOf(((DefaultTableModel) this.tblNotasAlunos.getModel()).getValueAt(i, 3));
                if (notaAluno.equals("") || notaAluno == null) {
                    notasEmBranco++;
                }
            }
            if (notasEmBranco > 0) {
                JOptionPane.showMessageDialog(this, "Atenção! Existe alunos sem notas lançadas.",
                        "Lançar Notas", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (cbNumeroBimestre.getModel().getSelectedItem().toString().equals("1º Bimestre")) {
                numBimestre = 1;
            } else {
                numBimestre = 2;
            }
            this.viewTurma.inserirNovoBimestre(this.turma, numBimestre);

            for (int i = 0; i < model.getRowCount(); i++) {
                String codigoMatricula = String.valueOf(((DefaultTableModel) this.tblNotasAlunos.getModel()).getValueAt(i, 0));
                String notaAluno = String.valueOf(((DefaultTableModel) this.tblNotasAlunos.getModel()).getValueAt(i, 3));
                this.viewTurma.lancarNotaAluno(turma, numBimestre, codigoMatricula, Float.valueOf(notaAluno));
            }
            this.viewTurma.fecharBimestre(turma, numBimestre);
            JOptionPane.showMessageDialog(this, "Notas lançadas com sucesso.",
                    "Lançar Notas", JOptionPane.INFORMATION_MESSAGE);
            limpaCampos();
        }
    }//GEN-LAST:event_btnLancarNotasActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnLancarNotas;
    private javax.swing.JButton btnSelecionarTurma;
    private javax.swing.JComboBox cbNumeroBimestre;
    private javax.swing.JTextField edtNomeMateria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNotasAlunos;
    // End of variables declaration//GEN-END:variables

    public void limpaCampos() {
        edtNomeMateria.setText("");
        DefaultTableModel model = (DefaultTableModel) this.tblNotasAlunos.getModel();
        for (int i = 0; i < model.getRowCount(); i++) {
            model.removeRow(i);
        }
    }
}
